﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Data.SqlClient;

// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "GetItemService" in code, svc and config file together.
public class GetItemService : IGetItemService
{
    public List<Item> getAll(String item_Name, Guid categoryID, Guid userID, float startBid, float endBid, int number)
    {
        try
        {
            List<Item> result1 = new List<Item>();
            DataClassesDataContext db = new DataClassesDataContext();
            if (categoryID != default(Guid))
            {
                List<Category> listCategory = getAllCategoryClild(categoryID);
                foreach (Category c in listCategory)
                {
                    List<Item_Table> itemList = db.Item_Tables.Where(q => ((q.Item_Title.Contains(item_Name) || item_Name.Equals("All")) && (q.Category_ID.Equals(c.Category_ID)))).Take(number).ToList();
                    foreach (Item_Table item in itemList)
                    {
                        result1.Add(new Item(item));
                    }
                }
            }
            else
            {
                List<Item_Table> itemList = db.Item_Tables.Where(q => ((q.Item_Title.Contains(item_Name) || item_Name.Equals("All")))).Take(number).ToList();
                foreach (Item_Table item in itemList)
                {
                    result1.Add(new Item(item));
                }
            }
            List<Item> result = (result1.Where(q => ((q.CurrentPrice >= startBid) || (q.CurrentPrice <= endBid)))).ToList();
            result = (result1.Where(q => ((q.CurrentPrice >= startBid) || (q.CurrentPrice <= endBid)))).ToList();
            return result;
        }
        catch (SqlException ex)
        {
        }
        catch (Exception ex)
        {
        }
        return null;
    }

    public Item getItemByID(Guid itemID)
    {
        try
        {
            DataClassesDataContext db = new DataClassesDataContext();
            Item_Table itb = db.Item_Tables.Where(q => q.Item_ID.Equals(itemID)).First();
            Item i = new Item(itb);
            i.getAllImage();
            i.getAllAuction();
            return i;
        }
        catch (SqlException ex)
        {
            return null;
        }
        catch (Exception ex)
        {
            return null;
        }
    }

    public List<Category> getAllCategory()
    {
        try
        {
            DataClassesDataContext db = new DataClassesDataContext();
            List<Category> result = new List<Category>();
            List<Category_Table> cts = db.Category_Tables.Where(q => q.CategoryStatus == true).ToList();
            foreach (Category_Table ct in cts)
            {
                result.Add(new Category(ct));
            }
            return result;
        }
        catch (SqlException ex)
        {
            return null;
        }
        catch (Exception ex)
        {
            return null;
        }
    }

    public List<Category> getAllCategoryClild(Guid c)
    {
        try
        {
            DataClassesDataContext db = new DataClassesDataContext();
            List<Category> result = new List<Category>();
            List<Category_Table> cts = db.Category_Tables.Where(q => (q.CategoryID.Equals(c))).ToList();
            result.Add(new Category(cts.First()));
            cts = db.Category_Tables.Where(q => (q.CategoryFather.Equals(c))).ToList();
            foreach (Category_Table ct in cts)
            {
                result.AddRange(getAllCategoryClild(ct.CategoryID));
            }
            return result;
        }
        catch (SqlException ex)
        {
            return null;
        }
        catch (Exception ex)
        {
            return null;
        }
    }
}
